using cocos2d;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace CT.HelloMonoCocos
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class TheGame : Game
    {
        GraphicsDeviceManager _graphics;
        SpriteBatch _spriteBatch;

        CCApplication _application;
        RenderTarget2D _offBuffer1;
        RenderTarget2D _offBuffer2;
        object _sync = new object();

        public TheGame()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            _graphics.SynchronizeWithVerticalRetrace = false;
            this.IsFixedTimeStep = false;  

            _graphics.ApplyChanges();

            IsMouseVisible = true;
            TouchPanel.EnableMouseTouchPoint = true;


            _application = new AppDelegate(this, _graphics);

            //this.Components.Add(application);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            _application.Initialize();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here

            _offBuffer1 = new RenderTarget2D(_graphics.GraphicsDevice, 
                _graphics.GraphicsDevice.PresentationParameters.Bounds.Width, _graphics.GraphicsDevice.PresentationParameters.Bounds.Height);

            _offBuffer2 = new RenderTarget2D(_graphics.GraphicsDevice,
                _graphics.GraphicsDevice.PresentationParameters.Bounds.Width, _graphics.GraphicsDevice.PresentationParameters.Bounds.Height);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }
        protected override void BeginRun()
        {
            this.GraphicsDevice.BlendState = BlendState.AlphaBlend;
            this.GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;

            base.BeginRun();
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            _application.Update(gameTime);

            base.Update(gameTime);

            //evgeni
            //implementing kind of "double buffering", in order to avoid "flicker" on weak tablets

            _graphics.GraphicsDevice.SetRenderTarget(_offBuffer2);
            _application.Draw(gameTime);
            _graphics.GraphicsDevice.SetRenderTarget(null);

            var temp = _offBuffer2;
            _offBuffer2 = _offBuffer1;
            _offBuffer1 = temp;
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            //GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin();

            _spriteBatch.Draw(_offBuffer1, Vector2.Zero, null, Color.White);

            _spriteBatch.End();


            base.Draw(gameTime);
        }
    }
}
